package mark.tom.fiter;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import mark.tom.fiter.bean.AggregateBean;
import mark.tom.fiter.bean.ResolvedAggregateBean;
import mark.tom.fiter.constants.StatusCodeEnums;

import static mark.tom.fiter.Utils.runOnMainThread;
import static mark.tom.fiter.constants.StatusCodeConstants.UNKNOWN_ERROR_CODE;

public class ConverterFactory extends ProcessConverter.Factory {

    @Override
    public ProcessConverter get() {
        return new DefaultConverter();
    }

    private final static class DefaultConverter implements ProcessConverter {

        @SuppressWarnings("unchecked")
        @Override
        public void convert(@Nullable Context mContext, final @Nullable byte[] data, final @Nullable IRequestListener requestListener) {
            // 二次判断以防内存泄漏
            if (stopGoon(mContext, requestListener)) {
                return;
            }
            runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    requestListener.onSuccess(data);
                }
            });
        }

        @SuppressWarnings("unchecked")
        @Override
        public void convert(@Nullable Context mContext,
                            @NonNull AggregateBean aggregateBean,
                            @Nullable final IRequestListener requestListener) {
            // 二次判断以防内存泄漏
            if (stopGoon(mContext, requestListener)) {
                return;
            }
            if (aggregateBean instanceof ResolvedAggregateBean) {
                ResolvedAggregateBean resolvedAggregate = (ResolvedAggregateBean) aggregateBean;
                final Object result = resolvedAggregate.getData();
                runOnMainThread(new Runnable() {
                    @Override
                    public void run() {
                        if (null == result) {
                            requestListener.onSuccess();
                        } else if (result instanceof List) {
                            requestListener.onSuccess((List) result);
                        } else {
                            requestListener.onSuccess(result);
                        }
                    }
                });
                return;
            }
            // 执行错误流程
            convert(mContext, null, UNKNOWN_ERROR_CODE, requestListener);
        }

        @Override
        public void convert(@Nullable Context mContext,
                            @Nullable final String msg,
                            @StatusCodeEnums final int statusCode,
                            @Nullable final IRequestListener<?> requestListener) {
            // 二次判断以防内存泄漏
            if (stopGoon(mContext, requestListener)) {
                return;
            }
            // 线程转换
            runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    requestListener.onFail(msg, statusCode);
                }
            });
        }

        private boolean stopGoon(@Nullable Context mContext, @Nullable IRequestListener<?> requestListener) {
            return null == mContext
                    || (mContext instanceof Activity && ((Activity) mContext).isFinishing())
                    || null == requestListener;
        }
    }
}
